//
//  AppDelegate.m
//  VPPublicUntilitisForPod
//
//  Created by verne on 2016/12/23.
//  Copyright © 2016年 vernepung. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseNavigationController.h"
#import "BaseViewController.h"
#import "ViewController.h"
#import "UIImage+Additional.h"
#import "VPHTTPRequestOperationManager.h"
#import "PathMacro.h"
#import "UtilsMacro.h"
#import <CoreTelephony/CTCellularData.h>
#import "VPPublicUntilitisHeader.h"
#import "PathMacro.h"
#import "DownTableViewCell.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

+ (AppDelegate *)shareAppDelegate {
    return (AppDelegate *)[UIApplication sharedApplication].delegate;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(comeHome:) name:@"UIApplicationDidEnterBackgroundNotification" object:nil];

    
    CTCellularData *cellularData = [[CTCellularData alloc]init];
    cellularData.cellularDataRestrictionDidUpdateNotifier =  ^(CTCellularDataRestrictedState state){
        //获取联网状态
        switch (state) {
            case kCTCellularDataRestricted:
                NSLog(@"Restricrted");
                break;
            case kCTCellularDataNotRestricted:
                NSLog(@"Not Restricted");
                break;
            case kCTCellularDataRestrictedStateUnknown:
                NSLog(@"Unknown");
                break;
            default:
                break;
        };
    };
    openUrlInSafari(@"tongxue://");
    openUrlInSafariWithCompleteBlock(@"qq://", nil, ^(BOOL success) {
        DLog(@"%zd", success);
    });
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { // iOS8
        UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];
        [application registerUserNotificationSettings:setting];
    }
    
    if (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
        // 这里添加处理代码
    }
    // Override point for customization after application launch.
    [BaseViewController regisiterWithBackgroundColor:[UIColor grayColor] navBackgroundImage:[UIImage imageWithColor:[UIColor purpleColor] size:CGSizeMake(1, 1)] tintColor:[UIColor redColor] titleTextAttributies:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName: [UIFont boldSystemFontOfSize:18]} shadowImage:[UIImage new] andBackButtonImageName:@"btn_nav_back" andItemTintColor:[UIColor greenColor]];
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.windowLevel = UIWindowLevelNormal;
    ViewController *vc = [ViewController new];
//    vc.statusBarStype = 
    vc.title = @"jajajaj";
    vc.navigationBarState = VPNavigationBarStateShow;
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    
    [[VPHTTPRequestOperationManager manager] configureBackroundSession];
    
    return YES;
}


- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
  completionHandler:(void (^)())completionHandler {
    [VPHTTPRequestOperationManager manager].backgroundSessionCompletionHandler = completionHandler;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)comeHome:(UIApplication *)application {
    NSLog(@"进入后台");
}

- (void)applicationWillTerminate:(UIApplication *)application {
    NSLog(@"程序被杀死");
    [[VPHTTPRequestOperationManager manager] save];
    
    [[NSFileManager defaultManager] createDirectoryAtPath:VPPathAtCacheWithDirectory(@"a") withIntermediateDirectories:YES attributes:nil error:nil];
    // 1.创建一个本地通知
    UILocalNotification *localNote = [[UILocalNotification alloc] init];
    // 1.2.设置通知内容
    localNote.alertBody = @"App被杀死";
    // 1.3.设置锁屏时,字体下方显示的一个文字
    localNote.alertAction = @"被杀死!!!!";
    localNote.hasAction = YES;
    
    // 1.5.设置通过到来的声音
    localNote.soundName = UILocalNotificationDefaultSoundName;
    
    // 1.6.设置应用图标左上角显示的数字
    localNote.applicationIconBadgeNumber = 1;
    // 2.执行通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
